﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Services.Description;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net.Mail;
using Microsoft.VisualBasic;
using Web.Prop;

namespace Web.Core
{
    public sealed class Email
    {
    
	public static bool Gmail(oProsonalMail mailFrom = null
        , List<oProsonalMail> mailTo = null
        , List<oProsonalMail> mailCc = null
        , List<oProsonalMail> mailBcc = null
        , oProsonalMail mailReply = null
        , string subject = null
        , string message = null
        , List<oAttachment> attachment = null
        , String mail = null
        , String port = null
        , String host = null
        , String username = null
        , String password = null)
	{
		try {
			MailMessage mailMsg = new MailMessage();
			// ""
			if (((mailFrom != null))) {
				mailMsg.From = (new MailAddress(mailFrom.Email, mailFrom.Name));
			}
			// ""
			if (((mailTo != null))) {
				if (((mailTo.Count > 0))) {
					foreach (oProsonalMail listTo in mailTo) {
						mailMsg.To.Add(new MailAddress(listTo.Email, listTo.Name));
					}
				}
			}

			// ""
			if (((mailCc != null))) {
				if (((mailCc.Count > 0) && (mailCc != null))) {
					foreach (oProsonalMail listCc in mailCc) {
						mailMsg.CC.Add(new MailAddress(listCc.Email, listCc.Name));
					}
				}
			}

			// ""
			if ((mailBcc != null)) {
				if (((mailBcc.Count > 0))) {
					foreach (oProsonalMail listBcc in mailBcc) {
						mailMsg.Bcc.Add(new MailAddress(listBcc.Email, listBcc.Name));
					}
				}
			}


			// ""
			if (((mailReply != null))) {
				mailMsg.ReplyTo = (new MailAddress(mailReply.Email, mailReply.Name));
			}
			// ""
			if (((attachment != null))) {
				if ((attachment.Count > 0)) {
					foreach (oAttachment _attach in attachment) {
						switch (_attach.ContentType.ToLower()) {
							case  // ERROR: Case labels with binary operators are unsupported : Equality
"memory/octet-stream":
								if ((_attach.Stream != null)) {
									mailMsg.Attachments.Add(new Attachment(_attach.Stream, _attach.SourcePath.ToString(), "application/octet-stream"));
								}
								break;
							default:
								if (!String.IsNullOrEmpty(_attach.SourcePath)) {
									mailMsg.Attachments.Add(new Attachment(_attach.SourcePath, _attach.ContentType));
								}
								break;
						}
					}
				}
			}
			// ""
			mailMsg.BodyEncoding = System.Text.Encoding.GetEncoding("windows-874");
			mailMsg.Subject = subject.Trim();
			mailMsg.Body = message.ToString() ;
			mailMsg.Priority = MailPriority.High;
			mailMsg.IsBodyHtml = true;
			// ""
			// "" Smtpclient to send the mail message
            System.Net.NetworkCredential cred = new System.Net.NetworkCredential(mail, password);//"siiwindeveop@gmail.com"
            System.Net.Mail.SmtpClient smtpMail = new System.Net.Mail.SmtpClient(host);//"smtp.gmail.com"
            smtpMail.Port = Int32.Parse(port);//587
			smtpMail.Credentials = cred;
			smtpMail.DeliveryMethod = SmtpDeliveryMethod.Network;
			//smtpMail.UseDefaultCredentials = False
		    smtpMail.Host = host;//"smtp.gmail.com";
			smtpMail.EnableSsl = true;
			smtpMail.Timeout = 15000;
			// "" send the mail message
			smtpMail.Send(mailMsg);
			return true;
		} catch (SmtpException mailEx) {
			throw mailEx;
		} catch (Exception ex) {
			throw ex;
		}
	}

	public static bool Mail(oProsonalMail mailFrom = null, List<oProsonalMail> mailTo = null, List<oProsonalMail> mailCc = null, List<oProsonalMail> mailBcc = null
        , oProsonalMail mailReply = null, string subject = null, string message = null, List<oAttachment> attachment = null
        , String mail = null
        , String port = null
        , String host = null
        , String username = null
        , String password = null)
	{
		try {
			MailMessage mailMsg = new MailMessage();
			// ""
			if (((mailFrom != null))) {
				mailMsg.From = (new MailAddress(mailFrom.Email, mailFrom.Name));
			}
			// ""
			if (((mailTo.Count > 0) && (mailTo != null))) {
				foreach (oProsonalMail listTo in mailTo) {
					mailMsg.To.Add(new MailAddress(listTo.Email, listTo.Name));
				}
			}
			// ""
			if (((mailCc.Count > 0) && (mailCc != null))) {
				foreach (oProsonalMail listCc in mailCc) {
					mailMsg.CC.Add(new MailAddress(listCc.Email, listCc.Name));
				}
			}
			// ""
			if (((mailBcc.Count > 0) && (mailBcc != null))) {
				foreach (oProsonalMail listBcc in mailBcc) {
					mailMsg.Bcc.Add(new MailAddress(listBcc.Email, listBcc.Name));
				}
			}
			// ""
			if (((mailReply != null))) {
				mailMsg.ReplyTo = (new MailAddress(mailReply.Email, mailReply.Name));
			}
			// ""
			if (((attachment != null))) {
				if ((attachment.Count > 0)) {
					foreach (oAttachment _attach in attachment) {
						switch (_attach.ContentType.ToLower()) {
							case  // ERROR: Case labels with binary operators are unsupported : Equality
                            "memory/octet-stream":
								if ((_attach.Stream != null)) {
									mailMsg.Attachments.Add(new Attachment(_attach.Stream, _attach.SourcePath.ToString(), "application/octet-stream"));
								}
								break;
							default:
								if (!String.IsNullOrEmpty(_attach.SourcePath)) {
									mailMsg.Attachments.Add(new Attachment(_attach.SourcePath, _attach.ContentType));
								}
								break;
						}
					}
				}
			}
			// ""
			mailMsg.BodyEncoding = System.Text.Encoding.GetEncoding("windows-874");
			mailMsg.Subject = subject.Trim();
			mailMsg.Body = message.ToString() ;
			mailMsg.Priority = MailPriority.High;
			mailMsg.IsBodyHtml = true;
			// ""
			// "" Smtpclient to send the mail message
			SmtpClient smtpMail = new SmtpClient();
			smtpMail.Port = 25;
			smtpMail.DeliveryMethod = SmtpDeliveryMethod.Network;
			smtpMail.UseDefaultCredentials = false;
			smtpMail.Host = "";
			smtpMail.Timeout = 15000;
			smtpMail.Send(mailMsg);
			return true;
		} catch (SmtpException mailEx) {
			throw mailEx;
		} catch (Exception ex) {
			throw ex;
		}
	}

	}

[Serializable()]
public sealed class oProsonalMail
{


	private string _Name;
	public string Name {
		get { return _Name; }
		set { _Name = value; }
	}


	private string _Email;
	public string Email {
		get { return _Email; }
		set { _Email = value; }
	}

}

[Serializable()]
public sealed class oAttachment
{


	private string _SourcePath;
	public string SourcePath {
		get { return _SourcePath; }
		set { _SourcePath = value; }
	}


	private string _ContentType;
	public string ContentType {
		get { return _ContentType; }
		set { _ContentType = value; }
	}


	private System.IO.Stream _Stream;
	/// <summary>
	/// if have stream reader Fixed Text "memory/octet-stream"
	/// </summary>
	/// <value></value>
	/// <returns></returns>
	/// <remarks></remarks>
	public System.IO.Stream Stream {
		get { return _Stream; }
		set { _Stream = value; }
	}


}

}
